package cc.rengu.redp.system.controller;


import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.system.domain.FileCharacter;
import cc.rengu.redp.system.service.FileManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/fileManager")
public class FileManagerController {

    @Autowired
    private FileManageService fileManageService;

    @GetMapping("/getFileList")
    public List<FileCharacter> getFileList(String currPath) {
        return fileManageService.getFileList(currPath);
    }

    @Log("在指定目录创建文件夹")
    @PutMapping("/createDirectory")
    public RedpResponse createDirectory(String currPath, String directoryName) {
        return fileManageService.createDirectory(currPath, directoryName);
    }

    @Log("删除目录或文件")
    @DeleteMapping("/deleteFileOrDirectory")
    public RedpResponse deleteFileOrDirectory(String[] fullNamesList) {
        List<String> fullNames = Arrays.asList(fullNamesList);
        return fileManageService.deleteFileOrDirectory(fullNames);
    }

    @Log("移动目录或文件")
    @PutMapping("/moveFileOrDirectory")
    public RedpResponse moveFileOrDirectory(String[] fullNamesList, String targetPath) {
        List<String> fullNames = Arrays.asList(fullNamesList);
        return fileManageService.moveFileOrDirectory(fullNames, targetPath);
    }

    @Log("重命名目录或文件")
    @PutMapping("/renameFileOrDirectory")
    public RedpResponse renameFileOrDirectory(String fullName, String targetName) {
        return fileManageService.renameFileOrDirectory(fullName, targetName);
    }

    @Log("文件下载")
    @PostMapping("/downloadFile")
    public RedpResponse downloadFile(String fullName, HttpServletResponse response) {
        String simpleName = fileManageService.getSimpleFileName(fullName);
        if (StringUtils.isBlank(simpleName)) {
            return RedpResponse.getInstance().failure().message("目标文件不存在");
        }

        try {
            String outputFileName = URLEncoder.encode(simpleName, "UTF-8").replaceAll("\\+", "%20");
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + outputFileName);
            response.setHeader("Access-Control-Expose-Headers", "File-Name");
            response.setHeader("File-Name", outputFileName);
            fileManageService.downloadFile(fullName, response.getOutputStream());
        } catch (IOException | RedpException e) {
            log.error("download error", e);
            return RedpResponse.getInstance().failure().message("文件下载失败");
        }

        return RedpResponse.getInstance().success().message("文件下载成功");
    }
}
